<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. 相机使用流程">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Android-Camera2使用">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Android/Android-Camera2使用/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. 相机使用流程">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-21f1d3c209a80bca.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T11:19:54.632Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android-Camera2使用">
<meta name="twitter:description" content="####1. 相机使用流程">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3110861-21f1d3c209a80bca.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Android/Android-Camera2使用/"/>





  <title>Android-Camera2使用 | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Android/Android-Camera2使用/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Android-Camera2使用</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. 相机使用流程<br><img src="https://upload-images.jianshu.io/upload_images/3110861-21f1d3c209a80bca.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.jpg"></p>
<a id="more"></a>
<p>####2. SurfaceView<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Android 5.0 使用Camera2 照相</span><br><span class="line"> * 使用SurfaceView显示</span><br><span class="line"> * 主要步骤：</span><br><span class="line"> * 1. 获得摄像头管理器CameraManager mCameraManager，mCameraManager.openCamera()来打开摄像头</span><br><span class="line"> * 2. 指定要打开的摄像头，并创建openCamera()所需要的CameraDevice.StateCallback stateCallback</span><br><span class="line"> * 3. 在CameraDevice.StateCallback stateCallback中调用takePreview()，这个方法中，使用CaptureRequest.Builder创建预览需要的CameraRequest，并初始化了CameraCaptureSession，最后调用了setRepeatingRequest(previewRequest, null, childHandler)进行了预览</span><br><span class="line"> * 4. 点击拍照按钮，调用takePicture()，这个方法内，最终调用了capture(mCaptureRequest, null, childHandler)</span><br><span class="line"> * 5. 在new ImageReader.OnImageAvailableListener()&#123;&#125;回调方法中，将拍照拿到的图片进行展示</span><br><span class="line"> */</span><br><span class="line">public class CameraSurfaceFragment extends Fragment &#123;</span><br><span class="line">  /**照相机设备请求码*/</span><br><span class="line">  private static final int REQUEST_CAMERA_CODE = 0x100;</span><br><span class="line">  /**拍照*/</span><br><span class="line">  private Button mBtnTake;</span><br><span class="line">  /**显示拍照好的图片*/</span><br><span class="line">  private ImageView mIvShow;</span><br><span class="line">  /**预览窗口*/</span><br><span class="line">  private SurfaceView mSurfaceView;</span><br><span class="line">  /**预览窗口Holder*/</span><br><span class="line">  private SurfaceHolder mSurfaceHolder;</span><br><span class="line">  /**子线程Handler*/</span><br><span class="line">  private Handler mChildHandler;</span><br><span class="line">  /**主线程Handler*/</span><br><span class="line">  private Handler mMainHandler;</span><br><span class="line">  /**照相机ID，标识前置，后置*/</span><br><span class="line">  private String mCameraId;</span><br><span class="line">  /**图片读取器*/</span><br><span class="line">  private ImageReader mImageReader;</span><br><span class="line">  /**摄像头管理者*/</span><br><span class="line">  private CameraManager mCameraManager;</span><br><span class="line">  /**照相机设备*/</span><br><span class="line">  private CameraDevice mCameraDevice;</span><br><span class="line">  /**照相会话*/</span><br><span class="line">  private CameraCaptureSession mCameraCaptureSession;</span><br><span class="line">  /**方向列表*/</span><br><span class="line">  private static final SparseIntArray ORIENTATIONS = new SparseIntArray();</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 摄像头状态监听</span><br><span class="line">   */</span><br><span class="line">  private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() &#123;</span><br><span class="line">    @Override</span><br><span class="line">    public void onOpened(@NonNull CameraDevice camera) &#123;</span><br><span class="line">      // 打开摄像头</span><br><span class="line">      mCameraDevice = camera;</span><br><span class="line">      // 开启预览</span><br><span class="line">      takePreview();</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    @Override</span><br><span class="line">    public void onDisconnected(@NonNull CameraDevice camera) &#123;</span><br><span class="line">      // 关闭摄像头</span><br><span class="line">      if (null != mCameraDevice) &#123;</span><br><span class="line">        // 关闭摄像头</span><br><span class="line">        mCameraDevice.close();</span><br><span class="line">        mCameraDevice = null;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    @Override</span><br><span class="line">    public void onError(@NonNull CameraDevice camera, int error) &#123;</span><br><span class="line">      // 摄像头异常</span><br><span class="line">      Toast.makeText(getContext(), &quot;摄像头开启失败&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br><span class="line">  </span><br><span class="line">  static &#123;</span><br><span class="line">    ORIENTATIONS.append(Surface.ROTATION_0, 90);</span><br><span class="line">    ORIENTATIONS.append(Surface.ROTATION_90, 0);</span><br><span class="line">    ORIENTATIONS.append(Surface.ROTATION_180, 270);</span><br><span class="line">    ORIENTATIONS.append(Surface.ROTATION_270, 180);</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  @Override</span><br><span class="line">  public View onCreateView(LayoutInflater inflater, ViewGroup container,</span><br><span class="line">                           Bundle savedInstanceState) &#123;</span><br><span class="line">    View root = inflater.inflate(R.layout.fragment_camera_surface, container, false);</span><br><span class="line">    initView(root);</span><br><span class="line">    initListener();</span><br><span class="line">    return root;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 初始化View</span><br><span class="line">   */</span><br><span class="line">  private void initView(View view) &#123;</span><br><span class="line">    // 绑定View</span><br><span class="line">    mIvShow = view.findViewById(R.id.iv_show);</span><br><span class="line">    mBtnTake = view.findViewById(R.id.btn_take);</span><br><span class="line">    mSurfaceView = view.findViewById(R.id.sv_camera);</span><br><span class="line">    // 获取Holder</span><br><span class="line">    mSurfaceHolder = mSurfaceView.getHolder();</span><br><span class="line">    // 设置屏幕常量</span><br><span class="line">    mSurfaceHolder.setKeepScreenOn(true);</span><br><span class="line">    // 设置SurfaceView回调</span><br><span class="line">    mSurfaceHolder.addCallback(new SurfaceHolder.Callback() &#123;</span><br><span class="line">      @Override</span><br><span class="line">      public void surfaceCreated(SurfaceHolder holder) &#123;</span><br><span class="line">        // SurfaceView 创建</span><br><span class="line">        initCamera();</span><br><span class="line">      &#125;</span><br><span class="line">  </span><br><span class="line">      @Override</span><br><span class="line">      public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) &#123;</span><br><span class="line">        // SurfaceView 改变</span><br><span class="line">      &#125;</span><br><span class="line">  </span><br><span class="line">      @Override</span><br><span class="line">      public void surfaceDestroyed(SurfaceHolder holder) &#123;</span><br><span class="line">        // SurfaceView 销毁</span><br><span class="line">        // 销毁照相机设备</span><br><span class="line">        if (null != mCameraDevice) &#123;</span><br><span class="line">          mCameraDevice.close();</span><br><span class="line">          mCameraDevice = null;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 初始化监听器</span><br><span class="line">   */</span><br><span class="line">  private void initListener() &#123;</span><br><span class="line">    mBtnTake.setOnClickListener(new View.OnClickListener() &#123;</span><br><span class="line">      @Override</span><br><span class="line">      public void onClick(View v) &#123;</span><br><span class="line">        takePicture();</span><br><span class="line">      &#125;</span><br><span class="line">      </span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 初始化照相机</span><br><span class="line">   */</span><br><span class="line">  private void initCamera() &#123;</span><br><span class="line">    // 创建Handler线程并启动</span><br><span class="line">    HandlerThread handlerThread = new HandlerThread(&quot;Camera&quot;);</span><br><span class="line">    handlerThread.start();</span><br><span class="line">    // 创建子线程Handler</span><br><span class="line">    mChildHandler = new Handler(handlerThread.getLooper());</span><br><span class="line">    // 创建主线程Handler</span><br><span class="line">    mMainHandler = new Handler(Looper.getMainLooper());</span><br><span class="line">    // 设置后置摄像头ID</span><br><span class="line">    mCameraId = String.valueOf(CameraCharacteristics.LENS_FACING_FRONT);</span><br><span class="line">    // 创建图片读取器</span><br><span class="line">    mImageReader = ImageReader.newInstance(1080, 1920, ImageFormat.JPEG, 1);</span><br><span class="line">    // 图片读取器设置图片可用监听</span><br><span class="line">    mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() &#123;</span><br><span class="line">      @Override</span><br><span class="line">      public void onImageAvailable(ImageReader reader) &#123;</span><br><span class="line">        showImage(reader);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;, mMainHandler);</span><br><span class="line">    // 获取摄像头管理</span><br><span class="line">    mCameraManager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);</span><br><span class="line">    // 打开摄像头</span><br><span class="line">    try &#123;</span><br><span class="line">      if (ActivityCompat.checkSelfPermission</span><br><span class="line">              (getContext(), Manifest.permission.CAMERA)</span><br><span class="line">              != PackageManager.PERMISSION_GRANTED) &#123;</span><br><span class="line">        // 申请权限</span><br><span class="line">        requestPermissions(new String[]&#123;Manifest.permission.CAMERA&#125;, REQUEST_CAMERA_CODE);</span><br><span class="line">      &#125; else &#123;</span><br><span class="line">        // 打开摄像头</span><br><span class="line">        mCameraManager.openCamera(mCameraId, mStateCallback, mMainHandler);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; catch (CameraAccessException e) &#123;</span><br><span class="line">      e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  @Override</span><br><span class="line">  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) &#123;</span><br><span class="line">    super.onRequestPermissionsResult(requestCode, permissions, grantResults);</span><br><span class="line">    if (REQUEST_CAMERA_CODE == requestCode) &#123;</span><br><span class="line">      // 权限允许</span><br><span class="line">      if (grantResults[0] == PackageManager.PERMISSION_GRANTED) &#123;</span><br><span class="line">        try &#123;</span><br><span class="line">          if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) &#123;</span><br><span class="line">            return;</span><br><span class="line">          &#125;</span><br><span class="line">          mCameraManager.openCamera(mCameraId, mStateCallback, mMainHandler);</span><br><span class="line">        &#125; catch (CameraAccessException e) &#123;</span><br><span class="line">          e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">      &#125; else &#123;</span><br><span class="line">        // 权限拒绝</span><br><span class="line">        Toast.makeText(getContext(), &quot;无权限&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 图片可用后，读取并显示图片</span><br><span class="line">   * @param reader 图片读取器</span><br><span class="line">   */</span><br><span class="line">  private void showImage(ImageReader reader) &#123;</span><br><span class="line">    // 拿到图片数据</span><br><span class="line">    Image image = reader.acquireNextImage();</span><br><span class="line">    // 获取字节缓冲</span><br><span class="line">    ByteBuffer buffer = image.getPlanes()[0].getBuffer();</span><br><span class="line">    // 创建与缓冲区相同的字节数组</span><br><span class="line">    byte[] bytes = new byte[buffer.remaining()];</span><br><span class="line">    // 将数据读取字节数组</span><br><span class="line">    buffer.get(bytes);</span><br><span class="line">    // 创建图片</span><br><span class="line">    final Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);</span><br><span class="line">    // 显示图片</span><br><span class="line">    if (null != bitmap) &#123;</span><br><span class="line">      mIvShow.setImageBitmap(bitmap);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 预览</span><br><span class="line">   */</span><br><span class="line">  private void takePreview() &#123;</span><br><span class="line">    try &#123;</span><br><span class="line">      // 创建预览需要的CaptureRequest.Builder</span><br><span class="line">      final CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);</span><br><span class="line">      // 将SurfaceView的surface作为Builder的目标</span><br><span class="line">      builder.addTarget(mSurfaceHolder.getSurface());</span><br><span class="line">      // 创建CameraCaptureSession,该对象负责管理处理预览请求和拍照请求</span><br><span class="line">      mCameraDevice.createCaptureSession(Arrays.asList(mSurfaceHolder.getSurface(), mImageReader.getSurface()), new CameraCaptureSession.StateCallback() &#123;</span><br><span class="line">        @Override</span><br><span class="line">        public void onConfigured(@NonNull CameraCaptureSession session) &#123;</span><br><span class="line">          // 检测设备是否为空</span><br><span class="line">          if (null == mCameraDevice) return;</span><br><span class="line">          // 配置</span><br><span class="line">          // 当摄像头已经准备好时，开始显示预览</span><br><span class="line">          mCameraCaptureSession = session;</span><br><span class="line">          try &#123;</span><br><span class="line">            // 自动对焦</span><br><span class="line">            builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);</span><br><span class="line">            // 打开闪光灯</span><br><span class="line">            builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);</span><br><span class="line">            // 显示预览</span><br><span class="line">            CaptureRequest request = builder.build();</span><br><span class="line">            // 会话设置重复请求</span><br><span class="line">            mCameraCaptureSession.setRepeatingRequest(request, null, mChildHandler);</span><br><span class="line">          &#125; catch (CameraAccessException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">  </span><br><span class="line">        @Override</span><br><span class="line">        public void onConfigureFailed(@NonNull CameraCaptureSession session) &#123;</span><br><span class="line">          Toast.makeText(getContext(), &quot;配置失败&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;, mChildHandler);</span><br><span class="line">    &#125; catch (CameraAccessException e) &#123;</span><br><span class="line">      e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 拍照</span><br><span class="line">   */</span><br><span class="line">  private void takePicture() &#123;</span><br><span class="line">    // 判断设备是否为空</span><br><span class="line">    if (null == mCameraDevice) return;</span><br><span class="line">    // 创建拍照需要的CaptureRequest.Builder</span><br><span class="line">    final CaptureRequest.Builder builder;</span><br><span class="line">    try &#123;</span><br><span class="line">      // 创建拍照请求</span><br><span class="line">      builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);</span><br><span class="line">      // 将imageReader的surface作为CaptureRequest.Builder的木白哦</span><br><span class="line">      builder.addTarget(mImageReader.getSurface());</span><br><span class="line">      // 自动对焦</span><br><span class="line">      builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);</span><br><span class="line">      // 自动曝光</span><br><span class="line">      builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);</span><br><span class="line">      // 获取手机方向</span><br><span class="line">      int rotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();</span><br><span class="line">      // 根据设备方向计算设置照片的方向</span><br><span class="line">      builder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));</span><br><span class="line">      // 拍照</span><br><span class="line">      CaptureRequest request = builder.build();</span><br><span class="line">      // 拍照会话执行拍照</span><br><span class="line">      mCameraCaptureSession.capture(request, null, mChildHandler);</span><br><span class="line">    &#125; catch (CameraAccessException e) &#123;</span><br><span class="line">      e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>####3. TextureView<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Camera2 与 Texture使用</span><br><span class="line"> */</span><br><span class="line">public class CameraTextureFragment extends Fragment &#123;</span><br><span class="line">  /**相机权限请求标识*/</span><br><span class="line">  private static final int REQUEST_CAMERA_CODE = 0x100;</span><br><span class="line">  /**预览*/</span><br><span class="line">  private TextureView mTextureView;</span><br><span class="line">  /**拍照按钮*/</span><br><span class="line">  private Button mBtnTake;</span><br><span class="line">  /**图片*/</span><br><span class="line">  private ImageView mImageView;</span><br><span class="line">  /**照相机ID，标识前置后置*/</span><br><span class="line">  private String mCameraId;</span><br><span class="line">  /**相机尺寸*/</span><br><span class="line">  private Size mCaptureSize;</span><br><span class="line">  /**图像读取者*/</span><br><span class="line">  private ImageReader mImageReader;</span><br><span class="line">  /**图像主线程Handler*/</span><br><span class="line">  private Handler mCameraHandler;</span><br><span class="line">  /**相机设备*/</span><br><span class="line">  private CameraDevice mCameraDevice;</span><br><span class="line">  /**预览大小*/</span><br><span class="line">  private Size mPreviewSize;</span><br><span class="line">  /**相机请求*/</span><br><span class="line">  private CaptureRequest.Builder mCameraCaptureBuilder;</span><br><span class="line">  /**相机拍照捕获会话*/</span><br><span class="line">  private CameraCaptureSession mCameraCaptureSession;</span><br><span class="line">  /**相机管理者*/</span><br><span class="line">  private CameraManager mCameraManager;</span><br><span class="line">  /**相机设备状态回调*/</span><br><span class="line">  private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() &#123;</span><br><span class="line">    @Override</span><br><span class="line">    public void onOpened(@NonNull CameraDevice camera) &#123;</span><br><span class="line">      // 打开</span><br><span class="line">      mCameraDevice = camera;</span><br><span class="line">      // 开始预览</span><br><span class="line">      takePreview();</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    @Override</span><br><span class="line">    public void onDisconnected(@NonNull CameraDevice camera) &#123;</span><br><span class="line">      // 断开连接</span><br><span class="line">      camera.close();</span><br><span class="line">      mCameraDevice = null;</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    @Override</span><br><span class="line">    public void onError(@NonNull CameraDevice camera, int error) &#123;</span><br><span class="line">      // 异常</span><br><span class="line">      camera.close();</span><br><span class="line">      mCameraDevice = null;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  @Override</span><br><span class="line">  public View onCreateView(LayoutInflater inflater, ViewGroup container,</span><br><span class="line">                           Bundle savedInstanceState) &#123;</span><br><span class="line">    View root = inflater.inflate(R.layout.fragment_camera_texture, container, false);</span><br><span class="line">    initView(root);</span><br><span class="line">    initListener();</span><br><span class="line">    return root;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 初始化View</span><br><span class="line">   */</span><br><span class="line">  private void initView(View view) &#123;</span><br><span class="line">    // 初始化View</span><br><span class="line">    mImageView = view.findViewById(R.id.iv_show);</span><br><span class="line">    mTextureView = view.findViewById(R.id.tv_camera);</span><br><span class="line">    mBtnTake = view.findViewById(R.id.btn_take);</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  @Override</span><br><span class="line">  public void onResume() &#123;</span><br><span class="line">    super.onResume();</span><br><span class="line">    mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() &#123;</span><br><span class="line">      @Override</span><br><span class="line">      public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) &#123;</span><br><span class="line">        // SurfaceTexture可用</span><br><span class="line">        // 设置相机参数并打开相机</span><br><span class="line">        setUpCamera(width, height);</span><br><span class="line">        openCamera();</span><br><span class="line">      &#125;</span><br><span class="line">  </span><br><span class="line">      @Override</span><br><span class="line">      public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) &#123;</span><br><span class="line">        // SurfaceTexture大小改变</span><br><span class="line">      &#125;</span><br><span class="line">  </span><br><span class="line">      @Override</span><br><span class="line">      public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) &#123;</span><br><span class="line">        // SurfaceTexture 销毁</span><br><span class="line">        return false;</span><br><span class="line">      &#125;</span><br><span class="line">  </span><br><span class="line">      @Override</span><br><span class="line">      public void onSurfaceTextureUpdated(SurfaceTexture surface) &#123;</span><br><span class="line">        // SurfaceTexture 更新</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 打开相机</span><br><span class="line">   */</span><br><span class="line">  private void openCamera() &#123;</span><br><span class="line">    // 获取照相机管理者</span><br><span class="line">    mCameraManager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);</span><br><span class="line">    try &#123;</span><br><span class="line">      if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) &#123;</span><br><span class="line">        requestPermissions(new String[]&#123;Manifest.permission.CAMERA&#125;, REQUEST_CAMERA_CODE);</span><br><span class="line">        return;</span><br><span class="line">      &#125;</span><br><span class="line">      // 打开相机</span><br><span class="line">      mCameraManager.openCamera(mCameraId, mStateCallback, mCameraHandler);</span><br><span class="line">    &#125; catch (CameraAccessException e) &#123;</span><br><span class="line">      e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  @Override</span><br><span class="line">  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) &#123;</span><br><span class="line">    super.onRequestPermissionsResult(requestCode, permissions, grantResults);</span><br><span class="line">    if (REQUEST_CAMERA_CODE == requestCode) &#123;</span><br><span class="line">      // 权限允许</span><br><span class="line">      if (grantResults[0] == PackageManager.PERMISSION_GRANTED) &#123;</span><br><span class="line">        try &#123;</span><br><span class="line">          if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) &#123;</span><br><span class="line">            return;</span><br><span class="line">          &#125;</span><br><span class="line">          mCameraManager.openCamera(mCameraId, mStateCallback, mCameraHandler);</span><br><span class="line">        &#125; catch (CameraAccessException e) &#123;</span><br><span class="line">          e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">      &#125; else &#123;</span><br><span class="line">        // 权限拒绝</span><br><span class="line">        Toast.makeText(getContext(), &quot;无权限&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 设置相机参数</span><br><span class="line">   * @param width 宽度</span><br><span class="line">   * @param height 高度</span><br><span class="line">   */</span><br><span class="line">  private void setUpCamera(int width, int height) &#123;</span><br><span class="line">    // 创建Handler</span><br><span class="line">    mCameraHandler = new Handler(Looper.getMainLooper());</span><br><span class="line">    // 获取摄像头的管理者</span><br><span class="line">    CameraManager cameraManager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);</span><br><span class="line">    try &#123;</span><br><span class="line">      // 遍历所有摄像头</span><br><span class="line">      for (String cameraId : cameraManager.getCameraIdList()) &#123;</span><br><span class="line">        // 相机特性</span><br><span class="line">        CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);</span><br><span class="line">        // 获取摄像头是前置还是后置</span><br><span class="line">        Integer facing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);</span><br><span class="line">        // 此处默认打开后置摄像头</span><br><span class="line">        if (null != facing &amp;&amp; CameraCharacteristics.LENS_FACING_FRONT == facing)  continue;</span><br><span class="line">        // 获取StreamConfigurationMap，管理摄像头支持的所有输出格式和尺寸</span><br><span class="line">        StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);</span><br><span class="line">        assert map != null;</span><br><span class="line">        // 根据TextureView的尺寸设置预览尺寸</span><br><span class="line">        mPreviewSize = getOptimalSize(map.getOutputSizes(SurfaceTexture.class), width, height);</span><br><span class="line">        // 获取相机支持的最大拍照尺寸</span><br><span class="line">        mCaptureSize = Collections.max(</span><br><span class="line">                Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), new Comparator&lt;Size&gt;() &#123;</span><br><span class="line">                  @Override</span><br><span class="line">                  public int compare(Size lhs, Size rhs) &#123;</span><br><span class="line">                    return Long.signum(lhs.getWidth() * lhs.getHeight() - rhs.getHeight() * rhs.getWidth());</span><br><span class="line">                  &#125;</span><br><span class="line">                &#125;);</span><br><span class="line">        // 此处ImageReader用于拍照所需</span><br><span class="line">        setupImageReader();</span><br><span class="line">        // 为摄像头赋值</span><br><span class="line">        mCameraId = cameraId;</span><br><span class="line">        break;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; catch (CameraAccessException e) &#123;</span><br><span class="line">      e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 设置ImageReader</span><br><span class="line">   */</span><br><span class="line">  private void setupImageReader() &#123;</span><br><span class="line">    // 2代表ImageReader中最多可以获取两帧图像流</span><br><span class="line">    mImageReader = ImageReader.newInstance(mCaptureSize.getWidth(), mCaptureSize.getHeight(), ImageFormat.JPEG, 1);</span><br><span class="line">    // 设置图像可用监听</span><br><span class="line">    mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() &#123;</span><br><span class="line">      @Override</span><br><span class="line">      public void onImageAvailable(ImageReader reader) &#123;</span><br><span class="line">        // 获取图片</span><br><span class="line">        final Image image = reader.acquireNextImage();</span><br><span class="line">        // 提交任务，保存图片</span><br><span class="line">        mCameraHandler.post(new ImageSaver(image));</span><br><span class="line">        // 更新UI</span><br><span class="line">        getActivity().runOnUiThread(new Runnable() &#123;</span><br><span class="line">          @Override</span><br><span class="line">          public void run() &#123;</span><br><span class="line">            // 获取字节缓冲区</span><br><span class="line">            ByteBuffer buffer = image.getPlanes()[0].getBuffer();</span><br><span class="line">            // 创建数组之前调用此方法，恢复默认设置</span><br><span class="line">            buffer.rewind();</span><br><span class="line">            // 创建与缓冲区内容大小相同的数组</span><br><span class="line">            byte[] bytes = new byte[buffer.remaining()];</span><br><span class="line">            // 从缓冲区存入字节数组,读取完成之后position在末尾</span><br><span class="line">            buffer.get(bytes);</span><br><span class="line">            // 获取Bitmap图像</span><br><span class="line">            final Bitmap bitmap =  BitmapFactory.decodeByteArray(bytes, 0, bytes.length);</span><br><span class="line">            // 显示</span><br><span class="line">            if (null != bitmap) &#123;</span><br><span class="line">              mImageView.setImageBitmap(bitmap);</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">          &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;, mCameraHandler);</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 选择SizeMap中大于并且最接近width和height的size</span><br><span class="line">   * @param sizeMap 可选的尺寸</span><br><span class="line">   * @param width 宽</span><br><span class="line">   * @param height 高</span><br><span class="line">   * @return 最接近width和height的size</span><br><span class="line">   */</span><br><span class="line">  private Size getOptimalSize(Size[] sizeMap, int width, int height) &#123;</span><br><span class="line">    // 创建列表</span><br><span class="line">    List&lt;Size&gt; sizeList = new ArrayList&lt;&gt;();</span><br><span class="line">    // 遍历</span><br><span class="line">    for (Size option : sizeMap) &#123;</span><br><span class="line">      // 判断宽度是否大于高度</span><br><span class="line">      if (width &gt; height) &#123;</span><br><span class="line">        if (option.getWidth() &gt; width &amp;&amp; option.getHeight() &gt; height) &#123;</span><br><span class="line">          sizeList.add(option);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125; else &#123;</span><br><span class="line">        if (option.getWidth() &gt; height &amp;&amp; option.getHeight() &gt; width) &#123;</span><br><span class="line">          sizeList.add(option);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    // 判断存储Size的列表是否有数据</span><br><span class="line">    if (sizeList.size() &gt; 0) &#123;</span><br><span class="line">      return Collections.min(sizeList, new Comparator&lt;Size&gt;() &#123;</span><br><span class="line">        @Override</span><br><span class="line">        public int compare(Size lhs, Size rhs) &#123;</span><br><span class="line">          return Long.signum(lhs.getWidth() * lhs.getHeight() - rhs.getWidth() * rhs.getHeight());</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">    return sizeMap[0];</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 设置监听</span><br><span class="line">   */</span><br><span class="line">  private void initListener() &#123;</span><br><span class="line">    mBtnTake.setOnClickListener(new View.OnClickListener() &#123;</span><br><span class="line">      @Override</span><br><span class="line">      public void onClick(View v) &#123;</span><br><span class="line">        takePicture();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 预览</span><br><span class="line">   */</span><br><span class="line">  private void takePreview()&#123;</span><br><span class="line">    // 获取SurfaceTexture</span><br><span class="line">    SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();</span><br><span class="line">    // 设置默认的缓冲大小</span><br><span class="line">    surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());</span><br><span class="line">    // 创建Surface</span><br><span class="line">    Surface previewSurface = new Surface(surfaceTexture);</span><br><span class="line">    try &#123;</span><br><span class="line">      // 创建预览请求</span><br><span class="line">      mCameraCaptureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);</span><br><span class="line">      // 将previewSurface添加到预览请求中</span><br><span class="line">      mCameraCaptureBuilder.addTarget(previewSurface);</span><br><span class="line">      // 创建会话</span><br><span class="line">      mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, mImageReader.getSurface()), new CameraCaptureSession.StateCallback() &#123;</span><br><span class="line">        @Override</span><br><span class="line">        public void onConfigured(@NonNull CameraCaptureSession session) &#123;</span><br><span class="line">          try &#123;</span><br><span class="line">            // 配置</span><br><span class="line">            CaptureRequest captureRequest = mCameraCaptureBuilder.build();</span><br><span class="line">            // 設置session</span><br><span class="line">            mCameraCaptureSession = session;</span><br><span class="line">            // 设置重复预览请求</span><br><span class="line">            mCameraCaptureSession.setRepeatingRequest(captureRequest, null, mCameraHandler);</span><br><span class="line">          &#125; catch (CameraAccessException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">  </span><br><span class="line">        @Override</span><br><span class="line">        public void onConfigureFailed(@NonNull CameraCaptureSession session) &#123;</span><br><span class="line">          // 配置失败</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;, mCameraHandler);</span><br><span class="line">    &#125; catch (CameraAccessException e) &#123;</span><br><span class="line">      e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 拍照</span><br><span class="line">   */</span><br><span class="line">  private void takePicture() &#123;</span><br><span class="line">    try &#123;</span><br><span class="line">      // 设置触发</span><br><span class="line">      mCameraCaptureBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);</span><br><span class="line">      mCameraCaptureBuilder.addTarget(mImageReader.getSurface());</span><br><span class="line">      // 拍照</span><br><span class="line">      mCameraCaptureSession.capture(mCameraCaptureBuilder.build(), null, mCameraHandler);</span><br><span class="line">    &#125; catch (CameraAccessException e) &#123;</span><br><span class="line">      Toast.makeText(getActivity(), &quot;异常&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">      e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 保存图片任务</span><br><span class="line">   */</span><br><span class="line">  private class ImageSaver implements Runnable &#123;</span><br><span class="line">    /**图像*/</span><br><span class="line">    private Image mImage;</span><br><span class="line">    ImageSaver(Image image) &#123;</span><br><span class="line">      this.mImage = image;</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    @Override</span><br><span class="line">    public void run() &#123;</span><br><span class="line">      // 获取字节缓冲区</span><br><span class="line">      ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();</span><br><span class="line">      // 创建数组之前调用此方法，恢复默认设置--重要</span><br><span class="line">      buffer.rewind();</span><br><span class="line">      // 创建与字节缓冲区大小相同的字节数组</span><br><span class="line">      byte[] data = new byte[buffer.remaining()];</span><br><span class="line">      // 将数据读取字节数组</span><br><span class="line">      buffer.get(data);</span><br><span class="line">      // 获取缓存路径</span><br><span class="line">      String path = getActivity().getExternalCacheDir().getPath();</span><br><span class="line">      // 获取时间戳</span><br><span class="line">      String timeStamp = new SimpleDateFormat(&quot;yyyyMMdd_HHmmss&quot;, Locale.CHINA).format(new Date());</span><br><span class="line">      // 文件名</span><br><span class="line">      String fileName = path + &quot;/IMG_&quot; + timeStamp + &quot;.jpg&quot;;</span><br><span class="line">      // 创建文件输出流</span><br><span class="line">      FileOutputStream fos = null;</span><br><span class="line">      try &#123;</span><br><span class="line">        // 初始化文件输出流</span><br><span class="line">        fos = new FileOutputStream(fileName);</span><br><span class="line">        // 将数据写入文件</span><br><span class="line">        fos.write(data, 0, data.length);</span><br><span class="line">        // 刷新缓冲区</span><br><span class="line">        fos.flush();</span><br><span class="line">      &#125; catch (IOException e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">      &#125; finally &#123;</span><br><span class="line">        if (null != fos) &#123;</span><br><span class="line">          try &#123;</span><br><span class="line">            fos.close();</span><br><span class="line">          &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>####4. 多功能相机</p>
<ul>
<li><p>切换摄像头</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 后置摄像头</span><br><span class="line"> */</span><br><span class="line">private static final int CAMERA_FRONT = CameraCharacteristics.LENS_FACING_FRONT;</span><br><span class="line">/**</span><br><span class="line"> * 前置摄像头</span><br><span class="line"> */</span><br><span class="line">private static final int CAMERA_BACK = CameraCharacteristics.LENS_FACING_BACK;</span><br><span class="line">/**</span><br><span class="line"> * 照相机ID，标识前置后置</span><br><span class="line"> */</span><br><span class="line">private String mCameraId;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 切换前后摄像头</span><br><span class="line"> */</span><br><span class="line">private void switchCamera() &#123;</span><br><span class="line">  if (String.valueOf(CAMERA_FRONT).equals(mCameraId)) &#123;</span><br><span class="line">    // 后置</span><br><span class="line">    mCameraId = String.valueOf(CAMERA_BACK);</span><br><span class="line">  &#125; else if (String.valueOf(CAMERA_BACK).equals(mCameraId)) &#123;</span><br><span class="line">    // 前置</span><br><span class="line">    mCameraId = String.valueOf(CAMERA_FRONT);</span><br><span class="line">  &#125;</span><br><span class="line">  closeCamera();</span><br><span class="line">  reOpenCamera();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 重新打开摄像头</span><br><span class="line"> */</span><br><span class="line">private void reOpenCamera() &#123;</span><br><span class="line">  if (mTextureView.isAvailable()) &#123;</span><br><span class="line">    openCamera();</span><br><span class="line">  &#125; else &#123;</span><br><span class="line">    setTextureListener();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 关闭摄像头</span><br><span class="line"> */</span><br><span class="line">private void closeCamera() &#123;</span><br><span class="line">  if (null != mCameraCaptureSession) &#123;</span><br><span class="line">    mCameraCaptureSession.close();</span><br><span class="line">    mCameraCaptureSession = null;</span><br><span class="line">  &#125;</span><br><span class="line">  if (null != mCameraDevice) &#123;</span><br><span class="line">    mCameraDevice.close();</span><br><span class="line">    mCameraDevice = null;</span><br><span class="line">  &#125;</span><br><span class="line">  if (null != mImageReader) &#123;</span><br><span class="line">    mImageReader.close();</span><br><span class="line">    mImageReader = null;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>延时拍摄</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 无延时</span><br><span class="line"> */</span><br><span class="line">private static final int DELAY_ZERO = 0;</span><br><span class="line">/**</span><br><span class="line"> * 延时3秒</span><br><span class="line"> */</span><br><span class="line">private static final int DELAY_THREE = 3;</span><br><span class="line">/**</span><br><span class="line"> * 延时10秒</span><br><span class="line"> */</span><br><span class="line">private static final int DELAY_TEN = 10;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 延时拍摄</span><br><span class="line"> */</span><br><span class="line">private void delayTake() &#123;</span><br><span class="line">  switch (mDelayTime) &#123;</span><br><span class="line">    case DELAY_ZERO:</span><br><span class="line">      mDelayTime = DELAY_THREE;</span><br><span class="line">      break;</span><br><span class="line">    case DELAY_THREE:</span><br><span class="line">      mDelayTime = DELAY_TEN;</span><br><span class="line">      break;</span><br><span class="line">    case DELAY_TEN:</span><br><span class="line">      mDelayTime = DELAY_ZERO;</span><br><span class="line">      break;</span><br><span class="line">  &#125;</span><br><span class="line">  mBtnDelay.setText(&quot;延时&quot; + mDelayTime + &quot;秒&quot;);</span><br><span class="line">  if (mDelayTime == DELAY_ZERO) &#123;</span><br><span class="line">    takePicture();</span><br><span class="line">  &#125; else &#123;</span><br><span class="line">    // 定时器</span><br><span class="line">    new CountDownTimer(mDelayTime * 1000, 1000) &#123;</span><br><span class="line">      </span><br><span class="line">      @Override</span><br><span class="line">      public void onTick(long millisUntilFinished) &#123;</span><br><span class="line">        mBtnDelay.setText(String.valueOf(millisUntilFinished/1000) + &quot;秒&quot;);</span><br><span class="line">      &#125;</span><br><span class="line">      </span><br><span class="line">      @Override</span><br><span class="line">      public void onFinish() &#123;</span><br><span class="line">        mBtnDelay.setText(&quot;延时&quot; + mDelayTime + &quot;秒&quot;);</span><br><span class="line">        takePicture();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;.start();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>切换闪光</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 闪光标识--自动开启</span><br><span class="line"> */</span><br><span class="line">private static final int FLASH_ZERO = 0;</span><br><span class="line">/**</span><br><span class="line"> * 闪光标识--总是开启</span><br><span class="line"> */</span><br><span class="line">private static final int FLASH_ONE = 1;</span><br><span class="line">/**</span><br><span class="line"> * 闪光标识--关闭</span><br><span class="line"> */</span><br><span class="line">private static final int FLASH_TWO = 2;</span><br><span class="line">/**</span><br><span class="line"> * 切换闪光模式</span><br><span class="line"> */</span><br><span class="line">private void switchFlash() &#123;</span><br><span class="line">  switch(mFlashMode) &#123;</span><br><span class="line">    case FLASH_ZERO:</span><br><span class="line">      mFlashMode = FLASH_ONE;</span><br><span class="line">      mCameraCaptureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);</span><br><span class="line">      break;</span><br><span class="line">    case FLASH_ONE:</span><br><span class="line">      mFlashMode = FLASH_TWO;</span><br><span class="line">      mCameraCaptureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);</span><br><span class="line">      break;</span><br><span class="line">    case FLASH_TWO:</span><br><span class="line">      mFlashMode = FLASH_ZERO;</span><br><span class="line">      mCameraCaptureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);</span><br><span class="line">      // 必须设置</span><br><span class="line">      mCameraCaptureBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);</span><br><span class="line">      break;</span><br><span class="line">  &#125;</span><br><span class="line">  try &#123;</span><br><span class="line">    mCameraCaptureSession.setRepeatingRequest(mCameraCaptureBuilder.build(), null, mCameraHandler);</span><br><span class="line">  &#125; catch (CameraAccessException e) &#123;</span><br><span class="line">    e.printStackTrace();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>####<a href="https://gitee.com/mazaiting/Article/tree/master/CameraDemo" target="_blank" rel="noopener">代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Android/Android-Animation(动画)---基础二(LayoutAnimationController)/" rel="next" title="Android-Animation(动画)---基础二(LayoutAnimationController)">
                <i class="fa fa-chevron-left"></i> Android-Animation(动画)---基础二(LayoutAnimationController)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Android/Android-CoordinatorLayout使用/" rel="prev" title="Android-CoordinatorLayout使用">
                Android-CoordinatorLayout使用 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
